/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scuecsz.view.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class ConditionCombin {
    //保留查询条件
    public static  String QSTRING="QSTRING";
    public static  String QPARAMS="QPARAMS";
    
    public static Map<String, String> getPageQueryParamsMap() {
        Map pageParams = FaceUtil.getPageRequestParameterMap();
        Map queryMap = new HashMap();
        Iterator<String> it = pageParams.keySet().iterator();
        String id = null;
        String iid = null;
        while (it.hasNext()) {
            id = it.next();
            int i = id.lastIndexOf(":") + 1;
            if (i >= 0) {
                iid = id.substring(i);
            }
            if (iid.startsWith("db")) {
                queryMap.put(iid, pageParams.get(id));
            }
        }
        return queryMap;
    }

    public static String combinNullCondtion(String column) {
        return " and ".concat(column).concat(" is null");
    }

    public static String combinStrCondtion(String column) {
        return " and ".concat(column).concat(" LIKE ?");
    }

    public static String combinFromCondtion(String column) {
        return " and ".concat(column).concat(">= ?");
    }

    public static String combinToCondtion(String column) {
        return " and ".concat(column).concat("<= ?");
    }

    public static String combinDateFromCondtion(String column) {
        return " and DATE_FORMAT(".concat(column).concat(",'%Y-%m-%d')>= ?");
    }

    public static String combinDateToCondtion(String column) {
        return " and DATE_FORMAT(".concat(column).concat(",'%Y-%m-%d')<= ?");
    }

    public static String combinStrCondtion(String column, String value, List values) {
        if (!value.equals("")) {
            values.add("%".concat(value).concat("%"));
            return combinStrCondtion(column);
        }
        return "";
    }

    public static String combinFromCondtion(String column, String value, List values) {
        if (!value.equals("")) {
            values.add(value);
            return combinFromCondtion(column);
        }
        return "";
    }

    public static String combinToCondtion(String column, String value, List values) {
        if (!value.equals("")) {
            values.add(value);
            return combinToCondtion(column);
        }
        return "";
    }

    public static String combinDateFromCondtion(String column, String value, List values) {
        if (!value.equals("")) {
            values.add(value);
            return combinDateFromCondtion(column);
        }
        return "";
    }

    public static String combinDateToCondtion(String column, String value, List values) {
        if (!value.equals("")) {
            values.add(value);
            return combinDateToCondtion(column);
        }
        return "";
    }

    public static String combinPageConditions(Map<String, String> pageQueryParamsMap, List values) {
        String condition = "";
        Iterator<String> it = pageQueryParamsMap.keySet().iterator();
        String column = null;
        while (it.hasNext()) {
            column = it.next();
            condition = condition.concat(combinPageCondition(column, pageQueryParamsMap.get(column), values));
        }
        return condition;
    }

    public static String combinPageConditions(List values) {
        String condition = "";
        Map pageParams = FaceUtil.getPageRequestParameterMap();
        Map queryMap = new HashMap();
        Iterator<String> it = pageParams.keySet().iterator();
        String id = null;
        String iid = null;
        while (it.hasNext()) {
            id = it.next();
            int i = id.lastIndexOf(":") + 1;
            if (i >= 0) {
                iid = id.substring(i);
            }
            if (iid.startsWith("db")) {
                queryMap.put(iid, pageParams.get(id));
                condition = condition.concat(combinPageCondition(iid,(String) pageParams.get(id), values));
            }
        }
        return condition;
    }

    public static String combinPageCondition(String id, String value, List values) {
        String condition = "";
        if (null == value) {
            condition = combinNullCondtion(id.replace("db_", "").replace("-", "."));
        } else {
            if (id.startsWith("db_")) {
                condition = combinStrCondtion(id.replace("db_", "").replace("-", "."), value, values);
            } else if (id.startsWith("dbfrom_")) {
                condition = combinFromCondtion(id.replace("dbfrom_", "").replace("-", "."), value, values);
            } else if (id.startsWith("dbto_")) {
                condition = combinToCondtion(id.replace("dbto_", "").replace("-", "."), value, values);
            } else if (id.startsWith("dbdatefrom_")) {
                condition = combinDateFromCondtion(id.replace("dbdatefrom_", "").replace("-", "."), value, values);
            } else if (id.startsWith("dbdateto_")) {
                condition = combinDateFromCondtion(id.replace("dbdateto_", "").replace("-", "."), value, values);
            }
        }
        return condition;
    }

    public static Map combinFirstQueryCondtion(Map condtionMap,List params) {
          if(null==condtionMap){
            condtionMap=new HashMap();
          }
          System.out.println("condtionMap"+condtionMap);
          if(null==params){
              params=new ArrayList();
            }else{
              params.clear();
            }
          condtionMap.put(QSTRING, combinPageConditions(params));
          condtionMap.put(QPARAMS, params);
          return condtionMap;
    }
}
